package com.bestv.engine.recommender;

import com.bestv.engine.model.RecommendationModel;
import com.bestv.utils.common.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Henry on 14-6-12.
 */
public class Model {
    private Logger logger = LoggerFactory.getLogger(getClass());

    String name;
    String size;
    String className;
    boolean isRandomSort;
    boolean isRandomSelect;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSize() {
        return size == null ? Constants.ITEMS_PER_MODEL : Integer.parseInt(size);
    }

    public void setSize(String size) {
        this.size = size;
    }

    public RecommendationModel getRecommendationModel() {
        RecommendationModel model = null;
        try {
            model = (RecommendationModel) Class.forName(className).newInstance();
            model.setRandomSort(isRandomSort);
            model.setRandomSelect(isRandomSelect);
        } catch (ClassNotFoundException e) {
            logger.error("Class {} not found.", className, e);
        } catch (InstantiationException e) {
            logger.error("Error on instantiation.", e);
        } catch (IllegalAccessException e) {
            logger.error("Error on illegal access.", e);
        }
        return model;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public boolean isRandomSort() {
        return isRandomSort;
    }

    public void setRandomSort(String isRandomSort) {
        this.isRandomSort = "true".equals(isRandomSort);
    }

    public boolean isRandomSelect() {
        return isRandomSelect;
    }

    public void setRandomSelect(String isRandomSelect) {
        this.isRandomSelect = "true".equals(isRandomSelect);
    }
}
